package com.shunli.cterminal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examples.core.enums.CommonStatusEnum;
import com.shunli.cterminal.entity.SystemProject;
import com.shunli.cterminal.entity.SystemTrafficRule;
import com.shunli.cterminal.entity.SystemTrafficRuleAllocRecord;
import com.shunli.cterminal.mapper.SystemTrafficRuleMapper;
import com.shunli.cterminal.model.bo.TrafficRuleAllocRecordBO;
import com.shunli.cterminal.model.res.TrafficValidRuleVO;
import com.shunli.cterminal.service.ISystemTrafficRuleAllocRecordService;
import com.shunli.cterminal.service.ISystemTrafficRuleService;
import com.shunli.cterminal.service.ProjectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流量时间策略 服务实现类
 * </p>
 *
 * @author daodao
 * @since 2025-04-16 18:01:51
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SystemTrafficRuleServiceImpl extends ServiceImpl<SystemTrafficRuleMapper, SystemTrafficRule> implements ISystemTrafficRuleService {

    private final ISystemTrafficRuleAllocRecordService systemTrafficRuleAllocRecordService;
    private final ProjectService projectService;

    /**
     * 新增分配记录
     *
     * @param trafficRuleAllocRecordBO 记录
     * @return String
     */
    @Override
    public String addAllotProjectRecord(TrafficRuleAllocRecordBO trafficRuleAllocRecordBO) {
        if (!trafficRuleAllocRecordBO.getAllocSuccess()) {
            return null;
        }
        SystemTrafficRuleAllocRecord systemTrafficRuleAllocRecord = new SystemTrafficRuleAllocRecord();
        systemTrafficRuleAllocRecord.setRuleId(trafficRuleAllocRecordBO.getRuleId());
        systemTrafficRuleAllocRecord.setAllocProjectCode(trafficRuleAllocRecordBO.getAllocProjectCode());
        systemTrafficRuleAllocRecord.setProjectCode(trafficRuleAllocRecordBO.getProjectCode());
        systemTrafficRuleAllocRecord.setOrderNo(trafficRuleAllocRecordBO.getOrderNo());
        systemTrafficRuleAllocRecord.setCreateTime(trafficRuleAllocRecordBO.getCreateTime());
        systemTrafficRuleAllocRecord.setUpdateTime(trafficRuleAllocRecordBO.getCreateTime());
        systemTrafficRuleAllocRecordService.save(systemTrafficRuleAllocRecord);
        return systemTrafficRuleAllocRecord.getId().toString();
    }

    @Override
    public TrafficValidRuleVO queryValidRule(String projectCode) {
        int currWeek = LocalDateTimeUtil.dayOfWeek(LocalDateTime.now().toLocalDate()).getIso8601Value();
        String currTime = LocalTime.now().format(DatePattern.NORM_TIME_FORMATTER);
        TrafficValidRuleVO trafficValidRuleVO = this.baseMapper.queryValidRule(projectCode, currWeek, currTime);
        if (ObjectUtil.isNull(trafficValidRuleVO)) {
            return null;
        }

        List<String> allotProjectCodeList = JSONObject.parseArray(trafficValidRuleVO.getAllotProjectCode(), String.class);
        List<String> projectCodeList = new ArrayList<>(allotProjectCodeList);
        projectCodeList.add(projectCode);
        Map<String, SystemProject> systemProjectMap = projectService.queryMap(projectCodeList);
        if (systemProjectMap.get(projectCode) == null
                || CommonStatusEnum.DISABLE.getCode().equals(systemProjectMap.get(projectCode).getStatus())) {
            return null;
        }

        allotProjectCodeList = this.checkProjectStatus(systemProjectMap, allotProjectCodeList);
        if (CollectionUtil.isEmpty(allotProjectCodeList)) {
            return null;
        }
        trafficValidRuleVO.setAllotProjectCodeList(allotProjectCodeList);
        return trafficValidRuleVO;
    }

    public List<String> checkProjectStatus(Map<String, SystemProject> systemProjectMap, List<String> allotProjectCodeList) {
        if (CollectionUtil.isEmpty(allotProjectCodeList)) {
            return allotProjectCodeList;
        }
        List<String> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(systemProjectMap) && CollectionUtil.isNotEmpty(allotProjectCodeList)) {
            for (String projectCode : allotProjectCodeList) {
                SystemProject systemProject = systemProjectMap.get(projectCode);
                if(ObjectUtil.isNotNull(systemProject)
                        && StrUtil.isNotBlank(systemProject.getStatus())
                        && CommonStatusEnum.ENABLE.getCode().equals(systemProject.getStatus())){
                    resultList.add(projectCode);
                }
            }
        }
        return resultList;
    }
}
